package com.sjw.workflow.agent;

import com.sjw.workflow.core.WorkflowContext;
// import org.slf4j.Logger;
// import org.slf4j.LoggerFactory;

import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;

/**
 * 数据处理Agent
 * 负责处理和分析输入数据
 */
public class DataProcessingAgent extends BaseAgent {
    
    // private static final Logger logger = LoggerFactory.getLogger(DataProcessingAgent.class);
    
    public DataProcessingAgent() {
        super("data-processor", "Data Processing Agent", 
              "Processes and analyzes input data using various algorithms");
    }
    
    @Override
    protected Map<String, Object> executeInternal(Map<String, Object> input, WorkflowContext context) throws Exception {
        System.out.println("DataProcessingAgent starting data processing");
        
        Map<String, Object> output = new HashMap<>();
        
        // 获取输入数据
        Object rawData = input.get("data");
        if (rawData == null) {
            throw new IllegalArgumentException("No data provided for processing");
        }
        
        // 模拟数据处理
        Map<String, Object> processedData = processData(rawData);
        
        // 生成分析报告
        Map<String, Object> analysis = generateAnalysis(processedData);
        
        // 准备输出
        output.put("processedData", processedData);
        output.put("analysis", analysis);
        output.put("dataType", rawData.getClass().getSimpleName());
        output.put("processingTime", System.currentTimeMillis());
        output.put("status", "success");
        
        // 模拟一些处理时间
        Thread.sleep(1000);
        
        System.out.println("DataProcessingAgent completed data processing");
        return output;
    }
    
    /**
     * 处理数据
     */
    private Map<String, Object> processData(Object rawData) {
        Map<String, Object> processed = new HashMap<>();
        
        if (rawData instanceof String) {
            String text = (String) rawData;
            processed.put("originalText", text);
            processed.put("wordCount", text.split("\\s+").length);
            processed.put("charCount", text.length());
            processed.put("processedText", text.toUpperCase());
            
        } else if (rawData instanceof List) {
            List<?> list = (List<?>) rawData;
            processed.put("originalList", list);
            processed.put("listSize", list.size());
            processed.put("processedList", new ArrayList<>(list));
            
        } else if (rawData instanceof Map) {
            Map<?, ?> map = (Map<?, ?>) rawData;
            processed.put("originalMap", map);
            processed.put("mapSize", map.size());
            processed.put("keys", new ArrayList<>(map.keySet()));
            
        } else {
            processed.put("originalData", rawData);
            processed.put("dataType", rawData.getClass().getSimpleName());
        }
        
        return processed;
    }
    
    /**
     * 生成分析报告
     */
    private Map<String, Object> generateAnalysis(Map<String, Object> processedData) {
        Map<String, Object> analysis = new HashMap<>();
        
        analysis.put("analysisType", "data_processing");
        analysis.put("timestamp", System.currentTimeMillis());
        analysis.put("summary", "Data has been successfully processed and analyzed");
        
        // 根据数据类型生成不同的分析
        if (processedData.containsKey("wordCount")) {
            analysis.put("textAnalysis", Map.of(
                "wordCount", processedData.get("wordCount"),
                "charCount", processedData.get("charCount"),
                "complexity", "medium"
            ));
        } else if (processedData.containsKey("listSize")) {
            analysis.put("listAnalysis", Map.of(
                "size", processedData.get("listSize"),
                "complexity", "low"
            ));
        } else if (processedData.containsKey("mapSize")) {
            analysis.put("mapAnalysis", Map.of(
                "size", processedData.get("mapSize"),
                "complexity", "high"
            ));
        }
        
        return analysis;
    }
}
